package com.exp.sys.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tk.mybatis.mapper.entity.Example;

import com.exp.base.action.EasyuiPage;
import com.exp.base.action.EasyuiPageInfo;
import com.exp.base.action.Message;
import com.exp.base.action.TreeItem;
import com.exp.sys.entity.Admin;
import com.exp.sys.entity.Department;
import com.exp.sys.service.department.DepartmentService;
import com.exp.sys.vo.department.DepartmentTreeVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/sys/department")
public class DepartmentAction extends BaseAction {
	@Resource
	DepartmentService departmentService;
	
	@RequestMapping("add")
	public String add(Model model, String parentId){
		Department department = new Department();
		department.setParentId(parentId);
		model.addAttribute("department", department);
		return "sys/department/department_input";
	}
	
	@RequestMapping("edit")
	public String edit(Model model,@ModelAttribute("id") String id){
		Department department = departmentService.get(id);
		model.addAttribute("department", department);
		return "sys/department/department_input";
	}
	
	@RequestMapping("list")
	public String list(){
		return "sys/department/department_list";
	}
	
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(HttpServletRequest request,Department department){
		List<String> errors = new ArrayList<String>();
		if(isValid(department, errors)){
		}
		if(errors.isEmpty()){
			Example example = new Example(Admin.class);
			example.createCriteria().andEqualTo("name", department.getName());
			if(departmentService.exist(example)){
				errors.add(StringUtils.join(department.getName(),"已存在",(department.isDeleted()?"（回收站）":"")));
			};
		}
		if(errors.isEmpty()){
			departmentService.save(department);
			return new Message(true);
		}
		return new Message(false,StringUtils.join(errors,","));
	}
	
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(HttpServletRequest request,Department department){
		List<String> errors = new ArrayList<String>();
		if(isValid(department, errors)){
			Department departmentEntity = departmentService.get(department.getId());
			String sourcesParentIds = departmentEntity.getParentIds();
			filterParameterMapIgnore(request, department, departmentEntity);
			departmentService.updateWithChildParentIds(departmentEntity,sourcesParentIds);
			return new Message(true);
		}else{
			return new Message(false,StringUtils.join(errors,","));
		}
	}
	
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(HttpServletRequest request, String id,boolean deleteChild){
		departmentService.delete(id,deleteChild);
		return new Message(true);
	}
	
//	@ResponseBody
//	@RequestMapping(value="softDelete")
//	public Message softDelete(HttpServletRequest request, String[] ids){
//		departmentService.softDelete(ids);
//		return new Message(true);
//	}
	
	@ResponseBody
	@RequestMapping("queryTree")
	public List<DepartmentTreeVo>  queryTree(Model model){
		List<DepartmentTreeVo>  treeItemList = departmentService.queryTree();
		return treeItemList;
	}
	
	
	@ResponseBody
	@RequestMapping("changeTreeNode")
	public Message changeTreeNode(String childId,String parentId){
		Assert.hasLength(childId, "childId不能为空");
		parentId = StringUtils.defaultString(parentId, "");
		departmentService.changeTreeNode(childId, parentId);
		return new Message(true);
	}
	
	
	
}
